package com.wn.controller;

import com.wn.bean.Building;
import com.wn.bean.R;
import com.wn.service.BuildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yellow Docter
 * @date 2022 -07-12
 * @desc
 */
@RestController   // == @ResponseBody + @Controller
@RequestMapping("build")
public class BuildController {

    @Autowired
    private BuildService buildService;

    @GetMapping
    public R findBuild(String currentPage,Integer pageSize){

        Integer currentP = 0;
        // 处理 currentPage字符串 转成integer的异常捕获
        try {
            currentP = Integer.parseInt(currentPage);
        }catch (NumberFormatException exception){
            // 如果异常 给默认值
            currentP = 1;
        }
        // 调用service的分页方法
        R build = buildService.findBuild(currentP, pageSize);
        return build;
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        R r = buildService.buildDeleteById(id);
        return r;
    }

    @DeleteMapping
    public R deleteRows(@RequestBody List<Integer> ids) throws RuntimeException{


        System.out.println("ids = " + ids);
        R  r = buildService.deleteRows(ids);

        return r;
    }

    /**
     * 通过id查询Builds
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findBuildsById(@PathVariable Integer id){
        R r = buildService.buildFindById(id);
        return r;
    }

    /**
     * 修改操作
     * 使用restful风格
     * 修改是put请求
     * @param id
     * @return
     */
    @PutMapping
    public R updateBuilding(@RequestBody Building build){
        R r = buildService.updateBuild(build);
        return r;
    }

    @PostMapping
    public R addBuilding(@RequestBody Building build){
        R r = buildService.addBuilding(build);
        return r;
    }
}
